/*
  Copyright (C) 2011, Kyungjoo Kim
  
  This file is part of UHM (Unassembled HyperMatrix) sparse direct solver.
  
  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  Also add information on how to contact you by electronic and paper mail.
  
  
  Kyungjoo Kim
  iamkyungjoo@gmail.com
*/
#include "uhm/common.hxx"
#include "uhm/util.hxx"
#include "uhm/mat.hxx"

#include "uhm/mat/linal.hxx"

namespace uhm {

  template<> void Mat_Linal_::create_flat_without_buffer() {
    _flat.create_without_buffer( type2linal(_type), _m, _n );
  }
  template<> void Mat_Linal_::create_flat_buffer() {
    if (_flat.is_created() && _flat.is_buffer_null()) {
      _flat.create_buffer();
      _flat.set_zero();
      Mat_Base_::create_flat_buffer();
    }
  }
  template<> void Mat_Linal_::free_flat_buffer() {
    if (_flat.is_created() && !_flat.is_buffer_null()) {
      _flat.free_buffer();
      Mat_Base_::free_flat_buffer();
    }
  }
  template<> void* Mat_Linal_::get_flat_buffer(Int_ offn) const {
    if (_flat.is_created() && !_flat.is_buffer_null() && offn < _flat.get_n()) {
      size_t byte_offset = (this->get_datatype_size()*_flat.get_cs()*offn);
      return (void*)((char*)_flat.get_buffer() + byte_offset);
    }
    return NULL;
  }
  template<> void Mat_Linal_::attach_flat_buffer(Int_ cs, Int_ rs, void *buffer) {
    _flat.attach_buffer(cs, rs, buffer);
  }
  template<> void Mat_Linal_::free_flat_without_buffer() {
    _flat.free_without_buffer();
  }
  template<> void Mat_Linal_::create_hier(Int_ mb, Int_ nb) {
    _hier.create(_flat, mb, nb);
  }
  template<> void Mat_Linal_::free_hier() {
    _hier.free();
  }
  template<> void Mat_Linal_::set_zero() {
    _flat.set_zero();
  }
  template<> void Mat_Linal_::set_identity() {
    _flat.set_identity();
  }
  template<> void Mat_Linal_::hermitianize() {
    _flat.hermitianize(uplo2linal(_uplo));
  }
  template<> void Mat_Linal_::transpose() {
    _flat.transpose();
  }
  template<> Bool_ Mat_Linal_::is_flat_buffer_null() {
    return _flat.is_buffer_null();
  }
}
